using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_forums_ForumPage : Controls.DataManagerForumControl
{
    private DAL.User CurrentUser
    {
        get
        {
            return Managers.UserLoginManager.CurrentUser;
        }
    }

    protected override void ItemChanged()
    {

        Refresh();
    }

    private void Refresh()
    {
        if (CurrentForum != null &&
            Helpers.ForumSecurityHelper.CurrentUserCanViewForum(CurrentForum))
        {
            mviewForum.SetActiveView(viewForum);
            Forumgrid1.CurrentForum = CurrentForum;
            if (Forumgrid1.GridHasItems)
            {
                pnlSubforums.Visible = true;
            }
            else
            {
                pnlSubforums.Visible = false;
            }


            lnkNewThread.NavigateUrl = String.Format("~/forums/newthread.aspx?f={0}", CurrentForum.ID);

            int currentPage;
            if (!Helpers.QueryStringHelper.TryGetInt32("page", out currentPage))
            {
                currentPage = 1;
            }
            Pager1.CurrentPage = currentPage;
            Pager1.UrlFormat = Helpers.StringHelper.SafeFormat("~/forums/forum.aspx?f={0}&page={{0}}", CurrentForum.ID);

            DAL.DataManagerPage<DAL.Thread> pagedItems = DAL.Threads.GetByForumAndPage(CurrentForum, currentPage);
            ForumThreads1.SetDataSource(pagedItems.Items);
            Pager1.TotalPages = pagedItems.Pages;

            Breadcrumb1.CurrentForum = CurrentForum;
        }
        else
        {
            mviewForum.SetActiveView(viewError);
        }
    }

    protected override void UserLoggedIn(object sender, EventArgs e)
    {
        Refresh();
    }

    protected override void UserLoggedOut(object sender, EventArgs e)
    {
        Refresh();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CurrentForum = GetItemFromQueryString();
        }
        if (CurrentForum != null &&
            Helpers.ForumSecurityHelper.CurrentUserCanViewForum(CurrentForum))
        {
            Page.Title = "FSTDT Forums: " + Helpers.StringHelper.SafeFormat(CurrentForum.Name);
        }
        else
        {
            Page.Title = "FSTDT Forums: " + "Forum not found.";
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (Managers.UserLoginManager.CurrentUser == null)
        {
            lnkNewThread.Visible = false;
        }
        else
        {
            lnkNewThread.Visible = true;
        }
    }
}
